import axios from "axios";
import {baseUrl} from './httpConfig'

// 文档  https://www.axios-http.cn/docs/req_config
// 理解参考 https://blog.csdn.net/qq_40649373/article/details/142057816
// ------------------------------------------
// 超时时间
export const timeoutLimit = 3600000;
// ------------------------------------------
// 请求对象
export const axiosInstances = axios.create({
    baseURL:baseUrl,
    timeout:timeoutLimit,
})
// ------------------------------------------
// 请求取消数组
// const cancelControllerMap
// ------------------------------------------
/**
 * 
 * @param {string} urlPathWithoutDomain 
 * @param {boolean} withCredentials 
 * @param {object} requestParameter 
 * @param {object} requestHeader 
 * @returns 
 */
export function httpGetBase(urlPathWithoutDomain,withCredentials = true,requestParameter = {},requestHeader ={}){
    console.log("httpGetBase 参数1:" + urlPathWithoutDomain + "<-->" + withCredentials + "<-->" + JSON.stringify(requestParameter) + "<-->" + JSON.stringify(requestHeader) + "<-->");
    return axiosInstances.request({
        method:'get',
        baseURL:baseUrl,
        timeout:timeoutLimit,
        url:urlPathWithoutDomain,
        withCredentials:withCredentials,
        params:requestParameter,
        headers:requestHeader,
    })
}

/**
 * 
 * @param {string} urlPathWithoutDomain 
 * @param {boolean} withCredentials 
 * @param {object} requestParameter 
 * @param {object} requestHeader 
 * @returns 
 */
export function httpPostBase(urlPathWithoutDomain,withCredentials = true,requestParameter = {},requestHeader ={}){
    console.log("httpPostBase 参数1:" + urlPathWithoutDomain + "<-->" + withCredentials + "<-->" + JSON.stringify(requestParameter) + "<-->" + JSON.stringify(requestHeader) + "<-->");
    return axiosInstances.request({
        method:'post',
        baseURL:baseUrl,
        timeout:timeoutLimit,
        url:urlPathWithoutDomain,
        withCredentials:withCredentials,
        params:requestParameter,
        headers:requestHeader,
    })
}
// ------------------------------------------
// 请求对象
/**
 * 
 * @param {string} urlPathWithoutDomain 
 * @param {boolean} withCredentials 
 * @param {object} requestParameter 
 * @param {object} requestHeader 
 * @returns 
 */
export function httpGet(urlPathWithoutDomain,withCredentials = true,requestParameter = {},requestHeader ={}){
    console.log("httpGet 参数1:" + urlPathWithoutDomain + "<-->");
    return new Promise(async (resolve)=>{
        console.log("httpGet 参数2:" + urlPathWithoutDomain + "<-->");
        const response = await httpGetBase(urlPathWithoutDomain,withCredentials,requestParameter,requestHeader);
        console.log("httpGet 结果1:" + response.status + "<-->");
        if (response.status == 200){
            console.log("httpGet 结果2:" + JSON.stringify(response.data) + "<-->");
            return JSON.stringify(response?.data??{});
        } else {
            console.log("httpGet 结果3:<-->");
            return ''
        }
    })
    // return httpGetBase(urlPathWithoutDomain,withCredentials,requestParameter,requestHeader);
}

/**
 * 
 * @param {string} urlPathWithoutDomain 
 * @param {boolean} withCredentials 
 * @param {object} requestParameter 
 * @param {object} requestHeader 
 * @returns 
 */
export function httpPost(urlPathWithoutDomain,withCredentials = true,requestParameter = {},requestHeader ={}){
    console.log("httpPost 参数1:" + urlPathWithoutDomain + "<-->");
    return new Promise(async (resolve)=>{
        console.log("httpPost 参数2:" + urlPathWithoutDomain + "<-->");
        const response = await httpPostBase(urlPathWithoutDomain,withCredentials,requestParameter,requestHeader);
        console.log("httpPost 结果1:" + response.status + "<-->");
        if (response.status == 200){
            console.log("httpPost 结果2:" + JSON.stringify(response.data) + "<-->");
            return JSON.stringify(response?.data??{});
        } else {
            console.log("httpPost 结果3:<-->");
            return ''
        }
    })
    // return httpGetBase(urlPathWithoutDomain,withCredentials,requestParameter,requestHeader);
}